﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using JobDescOnline.Presentation.Models;
using eq.CoreBase.View;
using eq.CoreBase.Control;

namespace JobDescOnline.Presentation.Controllers
{
    public class PerformanceIndicatorController : Controller
    {
        public JobDescDBEntities1 db = new JobDescDBEntities1();

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                PerformanceIndicator pi = db.PerformanceIndicators.CreateObject();
                if (ModelState.IsValid)
                {
                    pi.Rowstatus = true;
                    pi.JobDescResponsibilityID = Convert.ToInt32(collection["JobDescResponsibilityID"]);
                    pi.PI = collection["PI"];
                    pi.CreatedBy = UserAuthenticated.isAuthenticated ? UserAuthenticated.authUser.FirstName : "DEFAULT";
                    pi.CreatedOn = DateTime.Now;

                    db.PerformanceIndicators.AddObject(pi);
                    db.SaveChanges();
                }

                int id = Convert.ToInt32(collection["JobDescResponsibilityID"]);
                var model = db.PerformanceIndicators.Where(d => d.JobDescResponsibilityID == id);
                return View(model);
            }
            catch
            {
                return View();
            }
        }

        //
        // POST: /PerformanceIndicator/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                PerformanceIndicator PI = db.PerformanceIndicators.First(p => p.ID == id);
                if (ModelState.IsValid)
                {
                    PI.Rowstatus = true;
                    PI.PI = collection["PI"];
                    PI.LastModifiedBy = UserAuthenticated.isAuthenticated ? UserAuthenticated.authUser.FirstName : "DEFAULT";
                    PI.LastModifiedOn = DateTime.Now;

                    db.PerformanceIndicators.ApplyCurrentValues(PI);
                    db.SaveChanges();
                }

                var model = db.PerformanceIndicators.Where(d => d.JobDescResponsibilityID == PI.JobDescResponsibilityID);
                return View(model);
            }
            catch
            {
                return View();
            }
        }

        //
        // POST: /PerformanceIndicator/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                PerformanceIndicator pi = db.PerformanceIndicators.Single(d => d.ID == id);
                int ID = Convert.ToInt32(pi.JobDescResponsibilityID);
                db.PerformanceIndicators.DeleteObject(pi);

                var model = db.PerformanceIndicators.Where(d => d.JobDescResponsibilityID == ID);
                db.SaveChanges();
                return View(model);
            }
            catch
            {
                return View();
            }
        }
    }
}
